﻿using System;
using Ext.Net;
using Model;
using BLL;
using System.Web;

namespace Web.View
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ThemeChange();
        }
        private void ThemeChange()
        {
            if (Request.Cookies["theme"] != null)
            {
                switch (Request.Cookies["theme"].Value)
                {
                    case "gray":
                        HttpContext.Current.Session["Ext.net.Theme"] = Ext.Net.Theme.Gray;
                        break;
                    case "access":
                        HttpContext.Current.Session["Ext.net.Theme"] = Ext.Net.Theme.Access;
                        break;
                    case "slate":
                        HttpContext.Current.Session["Ext.net.Theme"] = Ext.Net.Theme.Slate;
                        break;
                    default:
                        HttpContext.Current.Session["Ext.net.Theme"] = Ext.Net.Theme.Default;
                        break;
                }
            }
        }
        protected void BtnLogin_Click(object sender, DirectEventArgs e)
        {
            if (TxtUserName.Text.Trim().Length <= 0 || TxtPwd.Text.Trim().Length <= 0)
                X.Msg.Alert("提示", "用户名和密码不能为空！").Show();
            else
            {
                userinfoMgr userinfoMgr = new userinfoMgr();
                userinfo user = userinfoMgr.FindById_userinfo(Server.HtmlEncode(TxtUserName.Text.Trim().Replace("'","")));
                //Md5+盐值加密算法，相对安全
                if (user != null && user.password.Trim() == CJ_DBOperater.CJ.PwdSecurity(TxtPwd.Text.Trim()))
                {
                    Session["currentUserID"] = user.userid;
                    Session["username"] = user.username;
                    Session["userpwd"] = user.password.Trim();
                    Session["role"] = user.roleid.ToString().Trim();
                    user.logintime = DateTime.Now.ToString();
                    userinfoMgr.Update_userinfo(user);//记录用户当前登录时间
                    Response.Redirect("/view/backMgr.aspx");
                }
                else
                {
                    X.Msg.Alert("提示", "用户名或密码错误！").Show();
                }
            }
        }

    }
}
